using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace Hematology.Data
{
	[DataObject]
	[Serializable]
	public partial class SiteContent
	{
		#region Constants

		private const string TableName = "[dbo].[SiteContent]";

		#endregion

		#region Properties

		public int? SiteContentId { get; set; }
		public int? ParentSiteContentId { get; set; }
		public string Title { get; set; }
		public string Content { get; set; }
		public int? ContentTypeCode { get; set; }
		public bool? IsDeleted { get; set; }
		public bool? IsVisible { get; set; }
		public DateTime? CreateDate { get; set; }
		public DateTime? LastModifiedDate { get; set; }

		#endregion

		#region Methods


		#region Mapping Methods

		protected void MapTo(DataSet ds)
		{
			if (ds == null)
				ds = new DataSet();

			if (ds.Tables[TableName] == null)
				ds.Tables.Add(TableName);

			ds.Tables[TableName].Columns.Add("SiteContentId", typeof(Int32));
			ds.Tables[TableName].Columns.Add("ParentSiteContentId", typeof(Int32));
			ds.Tables[TableName].Columns.Add("Title", typeof(String));
			ds.Tables[TableName].Columns.Add("Content", typeof(String));
			ds.Tables[TableName].Columns.Add("ContentTypeCode", typeof(Int32));
			ds.Tables[TableName].Columns.Add("IsDeleted", typeof(Boolean));
			ds.Tables[TableName].Columns.Add("IsVisible", typeof(Boolean));
			ds.Tables[TableName].Columns.Add("CreateDate", typeof(DateTime));
			ds.Tables[TableName].Columns.Add("LastModifiedDate", typeof(DateTime));

			DataRow dr = ds.Tables[TableName].NewRow();

			dr["SiteContentId"] = SiteContentId ?? (object)DBNull.Value;
			dr["ParentSiteContentId"] = ParentSiteContentId ?? (object)DBNull.Value;
			dr["Title"] = Title ?? (object)DBNull.Value;
			dr["Content"] = Content ?? (object)DBNull.Value;
			dr["ContentTypeCode"] = ContentTypeCode ?? (object)DBNull.Value;
			dr["IsDeleted"] = IsDeleted ?? (object)DBNull.Value;
			dr["IsVisible"] = IsVisible ?? (object)DBNull.Value;
			dr["CreateDate"] = CreateDate ?? (object)DBNull.Value;
			dr["LastModifiedDate"] = LastModifiedDate ?? (object)DBNull.Value;

			ds.Tables[TableName].Rows.Add(dr);
		}

		protected void MapFrom(DataRow dr)
		{
			SiteContentId = dr["SiteContentId"] != DBNull.Value ? Convert.ToInt32(dr["SiteContentId"]) : SiteContentId = null;
			ParentSiteContentId = dr["ParentSiteContentId"] != DBNull.Value ? Convert.ToInt32(dr["ParentSiteContentId"]) : ParentSiteContentId = null;
			Title = dr["Title"] != DBNull.Value ? Convert.ToString(dr["Title"]) : Title = null;
			Content = dr["Content"] != DBNull.Value ? Convert.ToString(dr["Content"]) : Content = null;
			ContentTypeCode = dr["ContentTypeCode"] != DBNull.Value ? Convert.ToInt32(dr["ContentTypeCode"]) : ContentTypeCode = null;
			IsDeleted = dr["IsDeleted"] != DBNull.Value ? Convert.ToBoolean(dr["IsDeleted"]) : IsDeleted = null;
			IsVisible = dr["IsVisible"] != DBNull.Value ? Convert.ToBoolean(dr["IsVisible"]) : IsVisible = null;
			CreateDate = dr["CreateDate"] != DBNull.Value ? Convert.ToDateTime(dr["CreateDate"]) : CreateDate = null;
			LastModifiedDate = dr["LastModifiedDate"] != DBNull.Value ? Convert.ToDateTime(dr["LastModifiedDate"]) : LastModifiedDate = null;

			FillChildSiteContent();
			FillFileAttachList();
			GetContentType();
		}

		public static List<SiteContent> MapFrom(DataSet ds)
		{
			// Initialise Collection.
			List<SiteContent> objects = new List<SiteContent>();

			// Validation.
			if (ds == null)
				throw new ApplicationException("Cannot map to dataset null.");

			if (ds.Tables[TableName].Rows.Count == 0)
				return objects;

			if (ds.Tables[TableName] == null)
				throw new ApplicationException("Cannot find table [dbo].[SiteContent] in DataSet.");

			if (ds.Tables[TableName].Rows.Count < 1)
				throw new ApplicationException("Table [dbo].[SiteContent] is empty.");

			// Map DataSet to Instance.
			foreach (DataRow dr in ds.Tables[TableName].Rows)
			{
				SiteContent instance = new SiteContent();
				instance.MapFrom(dr);
				instance.FillChildSiteContent();
				instance.FillFileAttachList();
				instance.GetContentType();
				objects.Add(instance);
			}

			// Return collection.
			return objects;
		}

		#endregion

		#region CRUD Methods

		public static SiteContent Get(Int32 siteContentId)
		{
			SiteContent instance = new SiteContent();

			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspSiteContent_SELECT";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, siteContentId))
			{
				using (DataSet ds = db.ExecuteDataSet(dbCommand))
				{
					if (ds == null || ds.Tables[0].Rows.Count == 0) throw new ApplicationException(String.Format("Could not get SiteContent ID:{0} from Database.", siteContentId));
					ds.Tables[0].TableName = TableName;

					instance.MapFrom(ds.Tables[0].Rows[0]);
				}
			}
			return instance;
		}

		#region INSERT
		private void Insert(Int32? parentSiteContentId, String title, String content, Int32? contentTypeCode, Boolean? isDeleted, Boolean? isVisible, DateTime? createDate, DateTime? lastModifiedDate, DbTransaction transaction = null)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspSiteContent_INSERT";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, parentSiteContentId, title, content, contentTypeCode, isDeleted, isVisible, createDate, lastModifiedDate))
			{
				SiteContentId = Convert.ToInt32(transaction == null ? db.ExecuteScalar(dbCommand) : db.ExecuteScalar(dbCommand, transaction));
			}
		}

		/// <summary>
		/// Insert current SiteContent to database.
		/// </summary>
		/// <param name="transaction">optional SQL Transaction</param>
		private void Insert(DbTransaction transaction = null)
		{
			Insert(ParentSiteContentId, Title, Content, ContentTypeCode, IsDeleted, IsVisible, CreateDate, LastModifiedDate, transaction);
		}

		#endregion

		#region UPDATE

		private static void Update(Int32? siteContentId, Int32? parentSiteContentId, String title, String content, Int32? contentTypeCode, Boolean? isDeleted, Boolean? isVisible, DateTime? createDate, DateTime? lastModifiedDate, DbTransaction transaction)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspSiteContent_UPDATE";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand))
			{
				db.DiscoverParameters(dbCommand);

				dbCommand.Parameters["@siteContentId"].Value = siteContentId;
				dbCommand.Parameters["@parentSiteContentId"].Value = parentSiteContentId;
				dbCommand.Parameters["@title"].Value = title;
				dbCommand.Parameters["@content"].Value = content;
				dbCommand.Parameters["@contentTypeCode"].Value = contentTypeCode;
				dbCommand.Parameters["@isDeleted"].Value = isDeleted;
				dbCommand.Parameters["@isVisible"].Value = isVisible;
				dbCommand.Parameters["@createDate"].Value = createDate;
				dbCommand.Parameters["@lastModifiedDate"].Value = lastModifiedDate;

				if (transaction == null)
					db.ExecuteNonQuery(dbCommand);
				else
					db.ExecuteNonQuery(dbCommand, transaction);
			}
		}

		private static void Update(Int32? siteContentId, Int32? parentSiteContentId, String title, String content, Int32? contentTypeCode, Boolean? isDeleted, Boolean? isVisible, DateTime? createDate, DateTime? lastModifiedDate)
		{
			Update(siteContentId, parentSiteContentId, title, content, contentTypeCode, isDeleted, isVisible, createDate, lastModifiedDate, null);
		}

		private static void Update(SiteContent siteContent)
		{
			siteContent.Update();
		}

		private static void Update(SiteContent siteContent, DbTransaction transaction)
		{
			siteContent.Update(transaction);
		}

		/// <summary>
		/// Updates changes to the database.
		/// </summary>
		/// <param name="transaction">optional SQL Transaction</param>
		private void Update(DbTransaction transaction)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspSiteContent_UPDATE";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand))
			{
				db.DiscoverParameters(dbCommand);

				dbCommand.Parameters["@siteContentId"].SourceColumn = "SiteContentId";
				dbCommand.Parameters["@parentSiteContentId"].SourceColumn = "ParentSiteContentId";
				dbCommand.Parameters["@title"].SourceColumn = "Title";
				dbCommand.Parameters["@content"].SourceColumn = "Content";
				dbCommand.Parameters["@contentTypeCode"].SourceColumn = "ContentTypeCode";
				dbCommand.Parameters["@isDeleted"].SourceColumn = "IsDeleted";
				dbCommand.Parameters["@isVisible"].SourceColumn = "IsVisible";
				dbCommand.Parameters["@createDate"].SourceColumn = "CreateDate";
				dbCommand.Parameters["@lastModifiedDate"].SourceColumn = "LastModifiedDate";

				using (DataSet ds = new DataSet())
				{
					MapTo(ds);
					ds.AcceptChanges();
					ds.Tables[0].Rows[0].SetModified();

					if (transaction == null)
						db.UpdateDataSet(ds, TableName, null, dbCommand, null, UpdateBehavior.Standard);
					else
						db.UpdateDataSet(ds, TableName, null, dbCommand, null, transaction);
				}
			}
		}

		/// <summary>
		/// Updates changes to the database.
		/// </summary>
		private void Update()
		{
			Update((DbTransaction)null);
		}

		#endregion

		#region DELETE

		public static void Delete(Int32? siteContentId, DbTransaction transaction)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspSiteContent_DELETE";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, siteContentId))
			{
				if (transaction != null)
				{
					db.ExecuteNonQuery(dbCommand, transaction);
				}
				else
				{
					db.ExecuteNonQuery(dbCommand);
				}
			}
		}

		public static void Delete(Int32? siteContentId)
		{
			Delete(
				siteContentId);
		}

		/// <summary>
		/// Delete current SiteContent from database.
		/// </summary>
		/// <param name="transaction">optional SQL Transaction</param>
		public void Delete(DbTransaction transaction)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspSiteContent_DELETE";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, SiteContentId))
			{
				if (transaction != null)
				{
					db.ExecuteNonQuery(dbCommand, transaction);
				}
				else
				{
					db.ExecuteNonQuery(dbCommand);
				}
			}
			SiteContentId = null;
		}

		/// <summary>
		/// Delete current SiteContent from database.
		/// </summary>
		public void Delete()
		{
			Delete((DbTransaction)null);
		}

		#endregion

		#region SEARCH

		public static List<SiteContent> Search(Int32? siteContentId = null, Int32? parentSiteContentId = null, String title = null, String content = null, Int32? contentTypeCode = null, Boolean? isDeleted = null, Boolean? isVisible = null, DateTime? createDate = null, DateTime? lastModifiedDate = null)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspSiteContent_SEARCH";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, siteContentId, parentSiteContentId, title, content, contentTypeCode, isDeleted, isVisible, createDate, lastModifiedDate))
			{
				using (DataSet ds = db.ExecuteDataSet(dbCommand))
				{
					ds.Tables[0].TableName = TableName;
					return MapFrom(ds);
				}
			}
		}

		public static List<SiteContent> Search(SiteContent searchObject)
		{
			return Search(searchObject.SiteContentId, searchObject.ParentSiteContentId, searchObject.Title, searchObject.Content,
						  searchObject.ContentTypeCode, searchObject.IsDeleted, searchObject.IsVisible, searchObject.CreateDate,
						  searchObject.LastModifiedDate);
		}

		/// <summary>
		/// Returns all SiteContent objects.
		/// </summary>
		/// <returns>List of all SiteContent objects. </returns>
		public static List<SiteContent> Search()
		{
			return Search(null, null, null, null, null, null, null, null, null);
		}

		#endregion

		#endregion

		#endregion
	}
}